<template>
  <div class="editor-page">
    <div class="container page">
      <div class="row">
        <div class="col-md-10 offset-md-1 col-xs-12">
          <form>
            <fieldset>
              <fieldset class="form-group">
                <input
                  :disabled="isAddArticle"
                  v-model="title"
                  required
                  type="text"
                  class="form-control form-control-lg"
                  placeholder="Article Title"
                />
              </fieldset>
              <fieldset class="form-group">
                <input
                  v-model="description"
                  :disabled="isAddArticle"
                  required
                  type="text"
                  class="form-control"
                  placeholder="What's this article about?"
                />
              </fieldset>
              <fieldset class="form-group">
                <textarea
                  class="form-control"
                  :disabled="isAddArticle"
                  v-model="body"
                  required
                  rows="8"
                  placeholder="Write your article (in markdown)"
                ></textarea>
              </fieldset>
              <fieldset class="form-group">
                <input
                  type="text"
                  :disabled="isAddArticle"
                  v-model="tag"
                  class="form-control"
                  placeholder="Enter tags"
                  @keyup="addTag"
                />
                <div class="tag-list">
                  <span class="tag-default tag-pill" v-for="(tag, index) in tagList" :key="index">
                    <i v-if="!isAddArticle" class="ion-close-round" @click="removeTag(tag)"></i>
                    {{tag}}
                  </span>
                </div>
              </fieldset>
              <button
                class="btn btn-lg pull-xs-right btn-primary"
                type="button"
                @click="insertArticle"
                :disabled="isAddArticle"
              >Publish Article</button>
            </fieldset>
          </form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { addArticle } from '@/api/article.js'
export default {
  name: 'Editor',
  middleware: "authenticated",
  data() {
    return {
      title: '',
      description: '',
      body: '',
      tagList: [],
      tag: '',

      isAddArticle: false,
    }
  },
  methods: {
    addTag(e) {
      if (this.tag.trim().length === 0) return
      if (this.tagList.includes(this.tag.trim())) return
      if (e.keyCode == '13') {
        this.tagList.push(this.tag.trim())
        this.tag = ''
      }
    },
    removeTag(tag) {
      this.tagList = this.tagList.filter((m) => m !== tag)
    },
    async insertArticle() {
      this.insertArticle = true
      const { data } = await addArticle({
        article: {
          title: this.title,
          description: this.description,
          body: this.body,
          tagList: this.tagList,
        },
      })
      this.isAddArticle = false
      if (data.article) {
        // 跳转到文章页面
        this.$router.push({
          name: 'article',
          params: {
            slug: data.article.slug,
          },
        })
      }
    },
  },
}
</script>